package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色分页
     *
     * @param dto      角色Dto
     * @param pageNum  书籍页码
     * @param pageSize 页面大小
     * @return {@code PageBean<RoleVo> }
     * @author YiSheep
     */
    @ApiOperation("角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<RoleVo>> pageQuery(@RequestBody RoleDto dto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("角色分页 dto:{},pageNum:{},pageSize:{}", dto, pageNum, pageSize);
        PageBean<RoleVo> pageBean =  roleService.pageQuery(dto, pageNum, pageSize);
        return ResponseResult.success(pageBean);
    }

    /**
     * 角色添加
     *
     * @param dto 角色Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult<Void> roleAdd(@RequestBody RoleDto dto) {
        log.info("角色添加 dto:{}", dto);
        roleService.roleAdd(dto);
        return ResponseResult.success();
    }


    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId 角色id
     * @return {@code ResponseResult<List<RoleVo>> }
     * @author YiSheep
     */
    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable Long roleId) {
        log.info("根据角色查询选中的资源数据 roleId:{}", roleId);
        Set<String> resourceNos = roleService.findCheckedResources(roleId);
        return ResponseResult.success(resourceNos);
    }

    /**
     * 角色修改
     *
     * @param dto 角色Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult<Void> roleUpdate(@RequestBody RoleDto dto) {
        log.info("角色修改 dto:{}", dto);
        roleService.roleUpdate(dto);
        return ResponseResult.success();
    }
    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> roleDelete(@PathVariable Long id) {
        log.info("删除角色 id:{}", id);
        roleService.roleDelete(id);
        return ResponseResult.success();
    }


    /**
     * 查询所有角色
     *
     * @return {@code ResponseResult<List<RoleVo>> }
     * @author YiSheep
     */
    @ApiOperation(value = "查询所有角色", notes = "角色下拉框")
    @PostMapping("/init-roles")
    public ResponseResult<List<RoleVo>> initRoles() {
        log.info("查询所有角色");
        List<RoleVo> roleVos = roleService.list();
        return ResponseResult.success(roleVos);
    }
}
